package com.lancoo.znbkxx.teacher.ui.activity

import android.content.Intent
import android.graphics.Typeface
import android.view.View
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.databinding.ActivityPaperBasicBinding
import com.lancoo.znbkxx.teacher.ui.fragment.BasicPaperFragment
import com.lancoo.znbkxx.teacher.ui.fragment.TabPaperFragment
import com.lancoo.znbkxx.teacher.viewmodel.PaperViewModel
import com.lancoo.znbkxx.teacher.widget.PaperFilterWindow

import kotlinx.android.synthetic.main.activity_paper_basic.*
import kotlinx.android.synthetic.main.view_filter_paper.view.*

/**
 * Created by wealive on 2021/3/25.
 *
 *
 */
class BasicPaperActivity : BaseVMActivity<PaperViewModel, ActivityPaperBasicBinding>(),
        View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private val layer by lazy { intent.getIntExtra("layer", -1) }
    private val filterPopupWindow by lazy {
        PaperFilterWindow(this, this, this).apply { setOnDismissListener { dismissAnimation() } }
    }
    private var publish = -1
    private var finish = -1
    private val tabFragment by lazy { TabPaperFragment() }
    private val basicPaperFragment by lazy {
        BasicPaperFragment().apply {
            arguments = bundleOf(Pair("layer", layer), Pair("from", "basic"))
        }
    }

    override fun providerVMClass(): Class<PaperViewModel>? {
        return PaperViewModel::class.java
    }

    override fun setLayoutID(): Int {
        return R.layout.activity_paper_basic
    }

    override fun initView() {
        iv_paper_icon.setImageResource(when (layer) {
            3 -> R.mipmap.icon_textbook
            4 -> R.mipmap.icon_topic
            -2 -> R.mipmap.icon_test_title
            else -> R.mipmap.icon_school_paper
        })
        tv_title.text = when (layer) {
            3 -> getString(R.string.textbook_review)
            4 -> getString(R.string.topic)
            -2 -> {
                when (CurrentUser.getGlobalGrade()) {
                    "K6", "K7", "K8", "K9" -> getString(R.string.test)
                    else -> getString(R.string.title_test)
                }
            }
            else -> getString(R.string.school_paper)
        }
        iv_empty.setImageResource(when (layer) {
            3 -> R.mipmap.aibk_paper_empty3
            4 -> R.mipmap.aibk_paper_empty4
            -2 -> R.mipmap.aibk_paper_empty2
            else -> R.mipmap.aibk_paper_empty5
        })
        tv_empty.typeface = Typeface.createFromAsset(assets, "fonts/happy.ttf")
        tv_empty.text = when (layer) {
            3 -> "暂无教材复习资料"
            4 -> "暂无专题复习资料"
            -2 -> "暂无${Ext.TESTNAME}复习资料"
            else -> "暂无校本化方案"
        }
        iv_search.setOnClickListener(this)
        iv_filter.setOnClickListener(this)
        iv_back.setOnClickListener(this)
        reSet()
    }

    private fun reSet() {
        (filterPopupWindow.contentView.rg_publish.getChildAt(0) as RadioButton).isChecked = true
        (filterPopupWindow.contentView.rg_finish.getChildAt(0) as RadioButton).isChecked = true
    }

    override fun startObserve() {
        viewModel.paperLayerInfo.observe(this, Observer {
            loadingDialog.show()
            if (it.MakeCount == 0) {
                group.visibility = View.VISIBLE
                loadingDialog.dismiss()
                return@Observer
            }
            tv_paper_num.text = it.MakeCount.toString()
            tv_paper_publish.text = it.ReleaseCount.toString()
            if (!tabFragment.isAdded && !basicPaperFragment.isAdded) {
                if (layer == -2) {
                    supportFragmentManager.beginTransaction().add(R.id.fcv_paper,
                            tabFragment).show(tabFragment).commitAllowingStateLoss()
                } else {
                    supportFragmentManager.beginTransaction().add(R.id.fcv_paper,
                            basicPaperFragment).show(basicPaperFragment).commitAllowingStateLoss()
                }
            }
        })
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> finish()
            R.id.iv_search -> {
                startActivity(Intent(this, SearchActivity::class.java).apply {
                    putExtra("layer", layer)
                    putExtra("publish", -1)
                    putExtra("finish", -1)
                })
            }
            R.id.iv_filter -> {
                filterPopupWindow.showAsDropDown(iv_filter, 0, 20)
                showAnimation()
            }
            R.id.iv_reset -> {
                reSet()
                publish = -1
                finish = -1
            }
            R.id.iv_config -> {
                filterPopupWindow.dismiss()
                startActivity(Intent(this, SearchActivity::class.java).apply {
                    putExtra("layer", layer)
                    putExtra("publish", publish)
                    putExtra("finish", finish)
                })
            }
        }
    }

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.rb_publish_all -> publish = -1
            R.id.rb_unpublished -> publish = 0
            R.id.rb_published -> publish = 1
            R.id.rb_finish_all -> finish = -1
            R.id.rb_unfinished -> finish = 0
            R.id.rb_finished -> finish = 1
        }
    }

    override fun onStart() {
        super.onStart()
        viewModel.getClassLayerPaperCount(layer)
    }
}